package models

import (
	"order-srv/basic/inits"
	"time"
)

type Goods struct {
	Id          uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	GoodsName   string    `gorm:"column:goods_name;type:varchar(50);comment:商品名称;not null;" json:"goods_name"`                                      // 商品名称
	GoodsPrice  float64   `gorm:"column:goods_price;type:decimal(10, 2);comment:商品价格;not null;default:0.00;" json:"goods_price"`                    // 商品价格
	BrandName   string    `gorm:"column:brand_name;type:varchar(50);comment:品牌名称;not null;" json:"brand_name"`                                      // 品牌名称
	GoodsUrl    string    `gorm:"column:goods_url;type:varchar(255);comment:商品图片;not null;" json:"goods_url"`                                       // 商品图片
	ShopId      uint32    `gorm:"column:shop_id;type:int UNSIGNED;comment:店铺id;not null;" json:"shop_id"`                                             // 店铺id
	SliderImage string    `gorm:"column:slider_image;type:varchar(2000);comment:轮播图;not null;" json:"slider_image"`                                  // 轮播图
	Keyword     string    `gorm:"column:keyword;type:varchar(255);comment:关键字;not null;" json:"keyword"`                                             // 关键字
	Sales       int32     `gorm:"column:sales;type:mediumint;comment:销量;not null;" json:"sales"`                                                      // 销量
	GoodsDesc   string    `gorm:"column:goods_desc;type:varchar(255);comment:商品简介;not null;" json:"goods_desc"`                                     // 商品简介
	GoodsStock  uint32    `gorm:"column:goods_stock;type:int UNSIGNED;comment:商品库存;not null;" json:"goods_stock"`                                   // 商品库存
	GoodsStatus uint8     `gorm:"column:goods_status;type:tinyint UNSIGNED;comment:商品状态 1-已上架 2-未上架;not null;default:1;" json:"goods_status"` // 商品状态 1-已上架 2-未上架
	AttrId      int32     `gorm:"column:attr_id;type:int;comment:属性id;not null;" json:"attr_id"`                                                      // 属性id
	CreateAt    time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt    time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt    time.Time `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (g *Goods) TableName() string {
	return "goods"
}

// 创建商品
func (g *Goods) CreateGoods() error {
	return inits.DB.Create(&g).Error
}

// 根据id查询商品
func (g *Goods) GetGoodsById(goodsId int) error {
	return inits.DB.Where("id = ?", goodsId).Limit(1).Find(&g).Error

}

// 主键id查询商品并联店铺名称及属性
func (g *Goods) GetGoodsByIdWithShop() (SelectSearchRes, error) {
	var k SelectSearchRes
	err := inits.DB.Model(&Goods{}).Select("`goods`.`id`,`goods`.`goods_name`,`goods`.`goods_price`,"+
		"`goods`.`brand_name`,`goods`.`goods_url`,`goods`.`shop_id`,`goods`.`keyword`,"+
		"`goods`.`sales`,`goods`.`goods_desc`,`goods`.`goods_stock`,"+
		"`goods`.`goods_status`,`shop`.`shop_name`,attr.color,attr.size,attr.material").
		Joins("join shop on goods.shop_id = shop.id").
		Joins("join attr on goods.attr_id = attr.id").
		Where("goods.id = ?", g.Id).Limit(1).Find(&k).Error
	return k, err
}
func (g *Goods) GoodsDetail() error {
	return inits.DB.Where("id = ?", g.Id).Find(&g).Error
}

type GoodsAttr struct {
}
type SelectSearchRes struct {
	GoodsName   string  ` json:"goods_name"`
	GoodsPrice  float64 ` json:"goods_price"`
	BrandName   string  ` json:"brand_name"`
	GoodsUrl    string  ` json:"goods_url"`
	ShopName    string  ` json:"shop_name"`
	SliderImage string  ` json:"slider_image"`
	Keyword     string  ` json:"keyword"`
	Sales       int32   ` json:"sales"`
	Color       string  `json:"color"`
	Size        string  `json:"size"`
	Material    string  `json:"material"`
	GoodsDesc   string  ` json:"goods_desc"`
	GoodsStock  uint32  ` json:"goods_stock"`
	GoodsStatus uint8   ` json:"goods_status"`
}

type GoodsInfo struct {
	Id          int64   `gorm:"column:id"`          //商品Id
	GoodsName   string  `gorm:"column:goods_name"`  //商品名称
	GoodsPrice  float64 `gorm:"column:goods_price"` //商品价格
	GoodsDesc   string  `gorm:"column:goods_desc"`  //商品简介
	ImageUrl    string  `gorm:"column:image_url"`   //图片
	SliderImage string  `gorm:"colum:slider_image"` //轮播图
}

// 商品详情
func (g *Goods) GetGoodsDetail(goodsId int64) (goodsInfo *GoodsInfo, err error) {
	err = inits.DB.Model(&Goods{}).
		Select("goods.id,goods.goods_name,goods.goods_price,goods.goods_desc,album.image_url,album.slider_image").
		Joins("left join album on album.goods_id=goods.id").
		Where("goods.id = ?", goodsId).Limit(1).Find(&goodsInfo).Error
	return goodsInfo, err
}

type ShopInfo struct {
	Id          int64   `gorm:"column:id"`           //商品Id
	GoodsName   string  `gorm:"column:goods_name"`   //商品名称
	GoodsPrice  float64 `gorm:"column:goods_price"`  //商品价格
	GoodsDesc   string  `gorm:"column:goods_desc"`   //商品简介
	ImageUrl    string  `gorm:"column:image_url"`    //图片
	SliderImage string  `gorm:"colum:slider_image"`  //轮播图
	ShopName    string  `gorm:"column:shop_name"`    //店铺名称
	ShopDesc    string  `gorm:"column:shop_desc"`    //店铺描述
	ShopLogo    string  `gorm:"column:shop_logo"`    //店铺logo
	ShopPhone   string  `gorm:"column:shop_phone"`   //店铺电话
	ShopAddress string  `gorm:"column:shop_address"` //店铺地址
	ShopStatus  int32   `gorm:"column:shop_status"`  //店铺 状态: 1正常 2禁用
}

// 店铺详情
func (g *Goods) GetShopDetail(goodsId int64) (shopInfo *ShopInfo, err error) {
	err = inits.DB.Model(&Goods{}).
		Select("goods.*,shop.shop_name,shop.shop_desc,shop.shop_logo,shop.shop_phone ,shop.shop_address,shop.shop_status,album.image_url,album.slider_image").
		Joins("left join shop on shop.id = goods.shop_id").
		Joins("left join album on album.goods_id=goods.id").
		Where("goods.id = ?", goodsId).Limit(1).
		First(&shopInfo).Error
	return shopInfo, err
}

type GoodsListInfo struct {
	Id          int64   `gorm:"column:id"`          //商品Id
	GoodsName   string  `gorm:"column:goods_name"`  //商品名称
	GoodsPrice  float64 `gorm:"column:goods_price"` //商品价格
	GoodsDesc   string  `gorm:"column:goods_desc"`  //商品简介
	ImageUrl    string  `gorm:"column:image_url"`   //图片
	SliderImage string  `gorm:"colum:slider_image"` //轮播图
	BrandName   string  `gorm:"colum:brand_name"`   //品牌
	Sales       int32   `gorm:"colum:sales"`        //销售量
}

// 商品推荐
func (g *Goods) GoodsList() (goodsListInfo []*GoodsListInfo, err error) {
	err = inits.DB.Model(&Goods{}).
		Select("goods.id,goods.goods_name,goods.goods_price,goods.goods_desc,goods.sales,goods.brand_name,album.image_url,album.slider_image").
		Joins("left join album on album.goods_id=goods.id").Order("sales desc").Find(&goodsListInfo).Error
	return goodsListInfo, err
}
